<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  <link rel="stylesheet" title="Default" href="http://acm.math.spbu.ru/~sk1/colorer/my.css">

  <script src="http://acm.math.spbu.ru/~sk1/colorer/highlight.js"></script>
  <script src="http://acm.math.spbu.ru/~sk1/colorer/cpp.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>

<body>

<pre><code>
/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com5
 * Date: 2013.10.18
 */

#include &lt;cstdio&gt;
#include &lt;cassert&gt;
#include &lt;algorithm&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

using namespace std;

#define forn(i, n) for (int i = 0; i &lt; (int)(n); i++)

const int N = 1 &lt;&lt; 10;
const int N2 = N * N;

char str[(int)1e6];
int n, n2, p[14][N2], ind[N][N], cur[N2], tmp[N2];
string s[] = {"id", "rot", "sym", "bhsym", "bvsym", "div", "mix"};

bool is( string a, string b )
{
  return a == b || a == b + "-";
}

int main()
{
  while (scanf("%d ", &n) && n)
  {
    gets(str);
    stringstream ss(str);
    string word;

    n2 = n * n;
    forn(i, n)
      forn(j, n)
        ind[i][j] = i * n + j;

    // id
    forn(i, n2)
      p[0][i] = i;

    // rot
    forn(i, n)
      forn(j, n)
        p[1][ind[i][j]] = ind[n - j - 1][i];

    // sym
    forn(i, n)
      forn(j, n)
        p[2][ind[i][j]] = ind[i][n - j - 1];

    // bhsym
    forn(i, n)
      forn(j, n)
        if (i &gt;= n / 2)
          p[3][ind[i][j]] = ind[i][n - j - 1];
        else
          p[3][ind[i][j]] = ind[i][j];

    // bvsym
    forn(i, n)
      forn(j, n)
        if (i &gt;= n / 2)
          p[4][ind[i][j]] = ind[(n / 2) + n - i - 1][j];
        else
          p[4][ind[i][j]] = ind[i][j];

    // div
    forn(i, n)
      forn(j, n)
        if (i & 1)
          p[5][ind[i][j]] = ind[n / 2 + i / 2][j];
        else
          p[5][ind[i][j]] = ind[i / 2][j];

    // mix
    forn(i, n)
      forn(j, n)
        if (j &lt; n / 2)
          p[6][ind[i][j]] = ind[i - (i & 1)][2 * j + (i & 1)];
        else
          p[6][ind[i][j]] = ind[i - (i & 1) + 1][2 * (j - (n / 2)) + (i & 1)];

    // inverse permutations
    forn(t, 7)
      forn(i, n2)
        p[t + 7][p[t][i]] = i;
    
    forn(i, n2)
      cur[i] = i;
    while (ss &gt;&gt; word)
    {
      int l = word.length(), i = 0;
      while (i &lt; 7 && !is(word, s[i]))
        i++;
      assert(i &lt; 7);
      if (word[l - 1] == '-')
        i += 7;

      forn(j, n2)
        tmp[j] = cur[p[i][j]];
      forn(j, n2)
        cur[j] = tmp[j];
    }

    int res = 1;
    forn(j, n2)
      tmp[j] = 0;
    forn(j, n2)
      if (!tmp[j])
      {
        int len = 0;
        for (int k = j; !tmp[k]; k = cur[k])
          len++, tmp[k] = 1;
        res *= len / __gcd(res, len);
      }
    printf("%d\n", res);
  }
  return 0;
}

</code></pre>

</body>
</html>

<font style="visibility:hidden">
